package com.gowants.pems.controller;

import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.mapper.AnalysisMapper;
import com.gowants.pems.model.AnalysisFault;
import com.gowants.pems.util.JSONMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/analysis/")
public class AnalysisController extends BaseController{

    @Autowired
    private AnalysisMapper mapper;

    @RequestMapping(value = "getAnalysisFault", method = RequestMethod.POST)
    @AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
    @ScmsAopLog(description = "故障统计", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap getAnalysisFault(String request, HttpServletRequest req){
        String code ="";
        if (!isPower("/analysis/getAnalysisFault.do"))
        {
            return doResponse(req, "TSMS_0000228");
        }
        try
        {
            JSONMap requestBody = getRequestBody(request);
            String station = requestBody.getString("station");
            Date startTime = requestBody.getDate("startTime");
            Date endTime = requestBody.getDate("endTime");
            int limit = requestBody.getInteger("limit");
            AnalysisFault analysisFault=new AnalysisFault();
            analysisFault.setOwner(getUser().getOwnerId());
            analysisFault.setStation(station);
            analysisFault.setStartTime(startTime);
            analysisFault.setEndTime(endTime);
            analysisFault.setLimit(limit);
            List<AnalysisFault> list = mapper.selectAnalysisFault(analysisFault);
            return doResponse(req, code, list);
        }
        catch (Exception e)
        {
            code = e.getMessage();
        }

        return doResponse(req, code, null);
    }


    @RequestMapping(value = "getAnalysisPart", method = RequestMethod.POST)
    @AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
    @ScmsAopLog(description = "设备一条记录检索", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap getAnalysisPart(String request, HttpServletRequest req){
        String code ="";
        if (!isPower("/analysis/getAnalysisPart.do"))
        {
            return doResponse(req, "TSMS_0000228");
        }
        try
        {
            JSONMap requestBody = getRequestBody(request);
            String station = requestBody.getString("station");
            String equipmentName = requestBody.getString("equipmentName");
            int limit = requestBody.getInteger("limit");
            AnalysisFault analysisFault=new AnalysisFault();
            analysisFault.setOwner(getUser().getOwnerId());
            analysisFault.setStation(station);
            analysisFault.setEquipmentName(equipmentName);
            analysisFault.setLimit(limit);
            List<AnalysisFault> list = mapper.selectAnalysisPart(analysisFault);
            return doResponse(req, code, list);
        }
        catch (Exception e)
        {
            code = e.getMessage();
        }

        return doResponse(req, code, null);
    }

    @RequestMapping(value = "getAnalysisPartUser", method = RequestMethod.POST)
    @AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
    @ScmsAopLog(description = "设备一条记录检索", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap getAnalysisPartUser(String request, HttpServletRequest req){
        String code ="";
        if (!isPower("/analysis/getAnalysisPartUser.do"))
        {
            return doResponse(req, "TSMS_0000228");
        }
        try
        {
            JSONMap requestBody = getRequestBody(request);
            String station = requestBody.getString("station");
            String equipmentName = requestBody.getString("equipmentName");
            int limit = requestBody.getInteger("limit");
            AnalysisFault analysisFault=new AnalysisFault();
            analysisFault.setOwner(getUser().getOwnerId());
            analysisFault.setStation(station);
            analysisFault.setEquipmentName(equipmentName);
            analysisFault.setLimit(limit);
            List<AnalysisFault> list = mapper.selectAnalysisPartUser(analysisFault);
            return doResponse(req, code, list);
        }
        catch (Exception e)
        {
            code = e.getMessage();
        }

        return doResponse(req, code, null);
    }


    @RequestMapping(value = "getEquipmentUserTime", method = RequestMethod.POST)
    @AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
    @ScmsAopLog(description = "设备一条记录检索", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap getEquipmentUserTime(String request, HttpServletRequest req){
        String code ="";
        if (!isPower("/analysis/getEquipmentUserTime.do"))
        {
            return doResponse(req, "TSMS_0000228");
        }
        try
        {
            JSONMap requestBody = getRequestBody(request);
            String station = requestBody.getString("station");
            int limit = requestBody.getInteger("limit");
            AnalysisFault analysisFault=new AnalysisFault();
            analysisFault.setOwner(getUser().getOwnerId());
            analysisFault.setStation(station);
            analysisFault.setLimit(limit);
            List<AnalysisFault> list = mapper.selectEquipmentUserTime(analysisFault);
            return doResponse(req, code, list);
        }
        catch (Exception e)
        {
            code = e.getMessage();
        }

        return doResponse(req, code, null);
    }
}
